package com.breakfast.fun.ui.user.order;

import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.breakfast.fun.Constant;
import com.breakfast.fun.R;
import com.breakfast.fun.base.BaseActivity;
import com.breakfast.fun.data.entity.mall.GoodsUserOrderItem;
import com.breakfast.fun.data.entity.mall.MallOrderDetail;
import com.breakfast.fun.data.entity.mall.MallOrderDetailData;
import com.breakfast.fun.data.entity.user.recharge.PayBean;
import com.breakfast.fun.event.RefreshEvent;
import com.breakfast.fun.ui.home.comment.CommentActivity;
import com.breakfast.fun.ui.user.feedback.OrderFeedBackActivity;
import com.breakfast.fun.ui.user.pay.BFPayActivity;
import com.breakfast.fun.util.JumpUtil;
import com.breakfast.fun.util.SharedUtil;
import com.breakfast.fun.view.widget.CustomDialog;
import com.breakfast.fun.view.widget.TitleBarView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.Bind;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

public class MallOrderDetailActivity extends BaseActivity<MallOrderDetailPresenter, MallOrderDetailModel> implements MallOrderDetailContract.View {

    @Bind(R.id.tbv_order_detail)
    TitleBarView mTbvOrderDetail;
    @Bind(R.id.tv_order_detail_state)
    TextView mTvOrderDetailState;
    @Bind(R.id.tv_order_detail_tip)
    TextView mTvOrderDetailTip;
    @Bind(R.id.tv_order_detail_name)
    TextView mTvOrderDetailName;
    @Bind(R.id.tv_order_detail_mobile)
    TextView mTvOrderDetailMobile;
    @Bind(R.id.tv_order_detail_address)
    TextView mTvOrderDetailAddress;
    @Bind(R.id.ll_order_detail)
    LinearLayout mLlOrderDetail;
    @Bind(R.id.tv_order_detail_price)
    TextView mTvOrderDetailPrice;
    @Bind(R.id.tv_order_detail_orderno)
    TextView mTvOrderDetailOrderno;
    @Bind(R.id.tv_order_detail_time)
    TextView mTvOrderDetailTime;
    @Bind(R.id.tv_order_detail_send_time)
    TextView mTvOrderDetailSendTime;
    @Bind(R.id.tv_order_detail_comment_time)
    TextView mTvOrderDetailCommentTime;
    @Bind(R.id.tv_order_detail_comment_score)
    TextView mTvOrderDetailCommentScore;
    @Bind(R.id.tv_order_detail_comment_content)
    TextView mTvOrderDetailCommentContent;
    @Bind(R.id.tv_order_detail_reply)
    TextView mTvOrderDetailReply;
    @Bind(R.id.ll_order_detail_comment)
    LinearLayout mLlOrderDetailComment;
    @Bind(R.id.sv_order_detail)
    ScrollView mSvOrderDetail;
    @Bind(R.id.ll_order_call)
    LinearLayout mLlOrderCall;
    @Bind(R.id.ll_order_feedback)
    LinearLayout mLlOrderFeedback;
    @Bind(R.id.ll_order_share)
    LinearLayout mLlOrderShare;
    @Bind(R.id.ll_order_detail_finish)
    LinearLayout mLlOrderDetailFinish;
    @Bind(R.id.tv_order_detail_cancel)
    TextView mTvOrderDetailCancel;
    @Bind(R.id.tv_order_detail_help)
    TextView mTvOrderDetailHelp;
    @Bind(R.id.tv_order_detail_again)
    TextView mTvOrderDetailAgain;
    @Bind(R.id.tv_order_detail_pay)
    TextView mTvOrderDetailPay;
    @Bind(R.id.tv_order_detail_comment)
    TextView mTvOrderDetailComment;
    @Bind(R.id.ll_order_detail_state)
    LinearLayout mLlOrderDetailState;
    @Bind(R.id.orderlist_lv_txt_empty)
    ImageView mOrderlistLvTxtEmpty;

    private String orderId;
    private String orderNo;

    private CustomDialog mDialog;
    private MallOrderDetailData data;

    @Override
    public int getLayoutId() {
        return R.layout.activity_mall_order_detail;
    }

    @Override
    public void initView() {
        orderId = getIntent().getStringExtra("orderId");
        orderNo = getIntent().getStringExtra("orderNo");

        mTbvOrderDetail.setTitleNoRight("订单详情");
        mTbvOrderDetail.setBack(this);
    }

    @Override
    public String getOrderId() {
        return orderId;
    }

    @Override
    public void showOrder(MallOrderDetail orderDetail) {
        data = orderDetail.getData();
        List<GoodsUserOrderItem> products = data.getGoods();

        mTvOrderDetailMobile.setText(data.getUserMobile());
        mTvOrderDetailName.setText(data.getConsignee());
        mTvOrderDetailAddress.setText(data.getDetail());
        mTvOrderDetailTime.setText(data.getOrderTime());
        mTvOrderDetailOrderno.setText(data.getOrderNo());
        mTvOrderDetailSendTime.setText(data.getSendTime());
        mTvOrderDetailPrice.setText(data.getTotalPrice() + "");

        mTvOrderDetailPrice.setText("￥" + String.format("%.2f", data.getTotalPrice()));

        if (data.getState() == 0) {
            //未付款
            mTvOrderDetailState.setText("未付款");
            mTvOrderDetailTip.setText("请在15分钟内完成订单");
            mLlOrderDetailState.setVisibility(View.VISIBLE);
            mTvOrderDetailCancel.setVisibility(View.VISIBLE);
            mTvOrderDetailPay.setVisibility(View.VISIBLE);
            mLlOrderDetailFinish.setVisibility(View.GONE);
            mTvOrderDetailComment.setVisibility(View.GONE);
            mLlOrderDetailComment.setVisibility(View.GONE);
            mTvOrderDetailHelp.setVisibility(View.GONE);
            mTvOrderDetailAgain.setVisibility(View.GONE);
        } else if (data.getState() == 1) {
            //未评价
            mTvOrderDetailState.setText("待评价");
            mTvOrderDetailTip.setText("来个评价吧,帮助我们做的更好");
            mLlOrderDetailState.setVisibility(View.VISIBLE);
            mTvOrderDetailCancel.setVisibility(View.GONE);
            mTvOrderDetailPay.setVisibility(View.GONE);
            mLlOrderDetailFinish.setVisibility(View.GONE);
            mTvOrderDetailComment.setVisibility(View.VISIBLE);
            mLlOrderDetailComment.setVisibility(View.GONE);
            mTvOrderDetailHelp.setVisibility(View.VISIBLE);
            mTvOrderDetailAgain.setVisibility(View.GONE);
        } else if (data.getState() == 2) {
            //已评价
            mTvOrderDetailState.setText("交易成功");
            mTvOrderDetailTip.setText("欢迎下次光临");
            mLlOrderDetailState.setVisibility(View.GONE);
            mTvOrderDetailCancel.setVisibility(View.GONE);
            mTvOrderDetailPay.setVisibility(View.GONE);
            mLlOrderDetailFinish.setVisibility(View.VISIBLE);
            mTvOrderDetailComment.setVisibility(View.GONE);
            if (data.getComment() != null) {
                mLlOrderDetailComment.setVisibility(View.VISIBLE);
                mTvOrderDetailCommentTime.setText(data.getComment().getAddTime());
                mTvOrderDetailCommentScore.setText(data.getComment().getAve() + "分");
                mTvOrderDetailCommentContent.setText(data.getComment().getContent());
                if (data.getComment().getReply() != null) {
                    mTvOrderDetailReply.setVisibility(View.VISIBLE);
                    mTvOrderDetailReply.setText("客服回复:" + data.getComment().getReply().getContent());
                } else {
                    mTvOrderDetailReply.setVisibility(View.GONE);
                }
            } else {
                mLlOrderDetailComment.setVisibility(View.GONE);
            }
            mTvOrderDetailHelp.setVisibility(View.VISIBLE);
            mTvOrderDetailAgain.setVisibility(View.GONE);
        }

        mTvOrderDetailHelp.setOnClickListener(v -> {
            JumpUtil.toHelp(context);
        });
        mTvOrderDetailAgain.setOnClickListener(v -> {
            JumpUtil.toHome(context);
        });
        mLlOrderCall.setOnClickListener(v -> {
            Uri uri = Uri.parse("tel:" + Constant.SERVICE_TEL);
            Intent intent = new Intent(Intent.ACTION_DIAL, uri);
            startActivity(intent);
        });
        mLlOrderFeedback.setOnClickListener(v -> {
            Intent intent = new Intent(context, OrderFeedBackActivity.class);
            intent.putExtra("orderId", data.getId() + "");
            startActivity(intent);
        });
        mLlOrderShare.setOnClickListener(v -> {
            showShare();
        });
        mTvOrderDetailCancel.setOnClickListener(v -> {
            mDialog = new CustomDialog(MallOrderDetailActivity.this, R.style.dialog_style,
                    R.layout.dialog_custom, "提示", "确定要取消订单吗？", true, false, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.dismiss();
                    presenter.cancelOrder();
                }
            });
            mDialog.show();
        });
        mTvOrderDetailComment.setOnClickListener(v -> {
            Intent intent = new Intent(context, CommentActivity.class);
            intent.putExtra("orderId", data.getId() + "");
            intent.putExtra("orderNo", data.getOrderNo());
            intent.putExtra("orderTime", data.getOrderTime());
            startActivity(intent);
        });

        mTvOrderDetailPay.setOnClickListener(v -> {
            presenter.getPayBean(orderNo);
        });

        View view;
        mLlOrderDetail.removeAllViews();
        for (int i = 0; i < products.size(); i++) {
            view = LayoutInflater.from(context).inflate(R.layout.item_order_detail, null);
            BFOrderDetailActivity.ViewHolder holder = new BFOrderDetailActivity.ViewHolder(view);
            imageUtil.loadUrlImage(products.get(i).getProductImage(), holder.mIvOrderItem);
            holder.mTvOrderItemName.setText(products.get(i).getProductName());
            holder.mTvOrderItemPrice.setText("￥" + products.get(i).getProductPrice() + "");
            holder.mTvOrderItemCount.setText("x" + products.get(i).getProductCount() + "");
            holder.mTvOrderItemTotal.setText("￥" + String.format("%.2f", products.get(i).getTotalPrice()) + "");
            mLlOrderDetail.addView(view);
        }
    }

    @Override
    public void showCancelSuccess() {
        EventBus.getDefault().post(new RefreshEvent("MallOrderFragment"));
        finish();
    }

    @Override
    public void toPay(PayBean payBean) {
        Intent intent = new Intent(this, BFPayActivity.class);
        intent.putExtra("payBean", payBean);
        intent.putExtra("isFromMall", true);
        startActivity(intent);
    }

    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();

        //关闭sso授权
        //oks.disableSSOWhenAuthorize();

        oks.setSilent(true);
// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle("早范邀请码:" + SharedUtil.getString(context, "recommendCode"));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://m.52letsgo.com/themes/default/saomiao.php");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("订健康早餐用早范，我为早范代言！我的邀请码是:" + SharedUtil.getString(context, "recommendCode"));
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl("http://static.52letsgo.cn/zz/ic_share.png");
        /*Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                + context.getResources().getResourcePackageName(R.drawable.ic_share) + "/" +
                context.getResources().getResourceTypeName(R.drawable.ic_share) + "/" +
                context.getResources().getResourceEntryName(R.drawable.ic_share));
        oks.setImagePath(uri.getPath());*/
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://m.52letsgo.com/themes/default/saomiao.php");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        //oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite("早范邀请码:" + SharedUtil.getString(context, "recommendCode"));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://m.52letsgo.com/themes/default/saomiao.php");

// 启动分享GUI
        oks.show(this);
    }

}
